package com.ddstation.crm.organization_structure.controller;

import com.ddstation.crm.institution.entity.DtHealthManageInstitutionDoctorEntity;
import com.ddstation.crm.login.bean.SessionLoginData;
import com.ddstation.crm.organization_structure.entity.DtInvsOrganizationStructureStaffEntity;
import com.ddstation.crm.organization_structure.service.StaffService;
import com.ddstation.doctor.entity.DdDoctorEntity;
import com.ddstation.security.util.WnBizMsgCrypt;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.inject.Inject;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 组织架构 （员工管理）
 * @author zhangdifeng
 * @date 2018年1月9日
 */
@Controller @Lazy
public class StaffController {

	@RequestMapping(value="config_department_user.do")
	public String initEvent(
			Model model,
			HttpSession session) {

		// 从session获取基础数据
		SessionLoginData loginData = (SessionLoginData)session.getAttribute(SessionLoginData.KEY);
		if (loginData == null || loginData.getDoctor() == null || loginData.getInstitution() == null) {
			return "forward:login?timeout=true"; // session 过期
		}

		String doctorUUID = WnBizMsgCrypt.encrypt(loginData.getDoctor().getSupervisorId().toString());
		model.addAttribute("doctorUUID", doctorUUID);
		return "pc/organization/organization";
	}
	
	/**
	 * list 组织架构中的staff
	 * @param session
	 * @return
	 */
	@RequestMapping(value="wncrm_org_struct.do/staff/list", method=RequestMethod.POST)
	public @ResponseBody List<DtInvsOrganizationStructureStaffEntity> listStaffEvent(
			@RequestParam("orgId")int orgId) {
		List<DtInvsOrganizationStructureStaffEntity> staffs = staffService.getCoverStaff(orgId);
		if (staffs != null) {
			for (DtInvsOrganizationStructureStaffEntity staff : staffs) {
				staff.setDoctorUUID(WnBizMsgCrypt.encrypt(staff.getDoctor().getId()));// doctorid 加密
				//隐藏名称前缀
				DtHealthManageInstitutionDoctorEntity account = staff.getAccount();
				String loginUser = account.getLoginUser();
				account.setLoginUser(loginUser.indexOf("invs_")==0?loginUser.substring(5,loginUser.length()):loginUser);
				staff.setAccount(account);
			}
		}
		return staffs;
	}
	
	
	//add by zhengxb 2018.4.9 start-----------------------------------
	/**
	 * list 组织架构中的staff(根据名字查询)
	 * @param orgId
	 * @param name 用户输入框中输入的名字
	 * @return
	 */
	@RequestMapping(value="wncrm_org_struct.do/staff/listbyname", method=RequestMethod.POST)
	public @ResponseBody List<DtInvsOrganizationStructureStaffEntity> listStaffByNameEvent(
			@RequestParam int orgId,
			@RequestParam String searchKeyName) {
		List<DtInvsOrganizationStructureStaffEntity> staffs = staffService.getCoverStaffByName(orgId,searchKeyName);
		if (staffs != null) {
			for (DtInvsOrganizationStructureStaffEntity staff : staffs) {
				staff.setDoctorUUID(WnBizMsgCrypt.encrypt(staff.getDoctor().getId()));// doctorid 加密
			}
		}
		return staffs;
	}
	//add by zhengxb 2018.4.9 end-----------------------------------
	
	@Inject
    StaffService staffService = null;
}
